/* 
 * (C) 2013 Daniel Garcia <contacto {at} danigarcia.org>
 * 
 * http://danigarcia.org
 * 
 * This file is part of BlackList.
 * 
 * BlackList is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * BlackList is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with BlackList.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package org.danigarcia.android.blacklist;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

/**
 * Provides an "About" dialog
 * @author Daniel Garcia [contacto {at} danigarcia.org]
 */
public class AboutDialog extends Dialog implements OnClickListener {

	private TextView lblWeb;
	private TextView lblMail;
	private Button btnCloseDialog;
	
	public AboutDialog(Context context) {
		super(context);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		configureDialog();
	}
	
	@Override
	public void onClick(View v) {
		if(v.getId() == R.id.btnCloseDialog)
			dismiss();
	}

	/**
	 * Basic configuration, init controls and listener association.
	 * Must be called one from onCreate()
	 */
	private void configureDialog()
	{
	    requestWindowFeature(Window.FEATURE_NO_TITLE);
	    setContentView(R.layout.dialog_about);
	    
	    btnCloseDialog = (Button) findViewById(R.id.btnCloseDialog);
	    
	    lblWeb = (TextView)findViewById(R.id.lblWeb);
	    lblMail = (TextView)findViewById(R.id.lblMail);
	    
	    String web = getContext().getString(R.string.developerWeb);
	    String mail = getContext().getString(R.string.developerMail);
	    
	    // Transform TextView into LinkLabel
	    makeHttpLinkTexView(lblWeb, web, web);
	    makeHttpLinkTexView(lblMail, "mailto:" + mail, mail);
	    	    
	    btnCloseDialog.setOnClickListener(this);

	}
	
	/**
	 * Transforms a regular TextView into a LinkButton
	 * @param textView TextView to transform
	 * @param url URL to access to. Precede with 'mailto:' if an e-mail is given
	 * @param text Text to show in the link
	 */
	private void makeHttpLinkTexView(TextView textView, String url, String text)
	{
		textView.setClickable(true);
		textView.setMovementMethod(LinkMovementMethod.getInstance());
		String httpAddress = "<a href='" + url +"'>" + text + "</a>";
		
		textView.setText(Html.fromHtml(httpAddress));
	}
}
